﻿namespace Vdc.Interface
{
    using Vdc.Interface.Interfaces;
    using System;
    using System.Collections;
    using System.IO;
    using System.Reflection;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public static class VdcControlFactory
    {
        public static void AddStyleSheetReferences(Control control)
        {
            IVdcControl control2 = control as IVdcControl;
            if (!IsInsideGrid(control) || ((!(control is VdcTextBox) || (((VdcTextBox)control).TextMode == TextBoxMode.MultiLine)) && !(control is VdcDropDownList)))
            {
                Type baseInterfaceType = GetBaseInterfaceType(control.GetType());
                if (IsDesignTimeRender(control) || !control2.StyleSheetReferenceAdded)
                {
                    string webResourceUrl;
                    if (!string.IsNullOrEmpty(control2.FolderStyle))
                    {
                        string str2 = "style.css";
                        if ((control is VdcTextBox) && (((VdcTextBox)control).TextMode == TextBoxMode.MultiLine))
                        {
                            str2 = "style_multiline.css";
                        }
                        webResourceUrl = Path.Combine(control2.FolderStyle, str2).Replace(@"\", "/");
                        if (webResourceUrl.IndexOf("~") == 0)
                        {
                            webResourceUrl = control.ResolveUrl(webResourceUrl);
                        }
                    }
                    else
                    {
                        webResourceUrl = control.Page.ClientScript.GetWebResourceUrl(baseInterfaceType, control2.LinkToEmbeddedStyleSheet);
                    }
                    if ((control.Page != null) && (control.Page.Header != null))
                    {
                        HtmlLink item = new HtmlLink();
                        item.Attributes.Add("type", "text/css");
                        item.Attributes.Add("rel", "stylesheet");
                        item.Href = webResourceUrl;
                        if ((IsDesignTimeRender(control) || !control2.ScriptManager.isAjax) || !control2.ScriptManager.IsInAsyncPostBack())
                        {
                            if (control2.HtmlLinksAddedToHead.Count > 0)
                            {
                                foreach (HtmlLink link2 in control2.HtmlLinksAddedToHead)
                                {
                                    control.Page.Header.Controls.Remove(link2);
                                }
                            }
                            control2.HtmlLinksAddedToHead.Add(item);
                            control.Page.Header.Controls.Add(item);
                        }
                        else
                        {
                            control2.ScriptManager.RegisterClientScriptBlock(baseInterfaceType.ToString() + "StyleSheet", JSHelper.AddStyleSheet(webResourceUrl), true);
                        }
                    }
                    else
                    {
                        HtmlLink child = new HtmlLink();
                        child.Attributes.Add("type", "text/css");
                        child.Attributes.Add("rel", "stylesheet");
                        child.Attributes.Add("display", "all");
                        child.Href = webResourceUrl;
                        if ((IsDesignTimeRender(control) || !control2.ScriptManager.isAjax) || !control2.ScriptManager.IsInAsyncPostBack())
                        {
                            control2.Container.Controls.Add(child);
                        }
                        else
                        {
                            control2.ScriptManager.RegisterClientScriptBlock(baseInterfaceType.ToString() + "StyleSheet", JSHelper.AddStyleSheet(webResourceUrl), true);
                        }
                    }
                    if (!IsDesignTimeRender(control))
                    {
                        control2.StyleSheetReferenceAdded = true;
                    }
                }
            }
        }

        internal static bool CheckAjaxPageInheritance(Control control)
        {
            Type baseType = control.Page.GetType().BaseType;
            while (baseType != null)
            {
                if (baseType.ToString() == "OboutInc.oboutAJAXPage")
                {
                    return true;
                }
                try
                {
                    baseType = Activator.CreateInstance(baseType).GetType().BaseType;
                    continue;
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                    return false;
                }
            }
            return false;
        }

        public static void CreateChildControls(Control control)
        {
            IVdcControl vdcControl = control as IVdcControl;
            AddStyleSheetReferences(control);
            CreateClientScriptContainer(vdcControl);
        }

        public static void CreateClientScriptContainer(IVdcControl vdcControl)
        {
            VdcDiv child = new VdcDiv();
            child.Style["display"] = "none";
            child.CssClass = "ob_iCallbackScript";
            vdcControl.ScriptContainerContent = new Literal();
            child.Controls.Add(vdcControl.ScriptContainerContent);
            vdcControl.Container.Controls.Add(child);
        }

        internal static VdcDiv CreateSection(Control control, string cssClass)
        {
            IVdcControl control2 = control as IVdcControl;
            VdcDiv child = new VdcDiv
            {
                CssClass = cssClass
            };
            control2.Container.Controls.Add(child);
            return child;
        }

        internal static Type GetBaseInterfaceType(Type t)
        {
            if (t.BaseType.ToString().IndexOf("Vdc.Interface") == -1)
            {
                return t;
            }
            return GetBaseInterfaceType(t.BaseType);
        }

        public static string GetClientObjectID(Control control)
        {
            IVdcControl control2 = control as IVdcControl;
            return GetClientObjectID(control, control2.AvailableClientIds);
        }

        public static string GetClientObjectID(Control control, Hashtable clientIds)
        {
            string key = ((control.ID != null) && !control.ID.Equals(string.Empty)) ? control.ID : control.ClientID;
            if (clientIds.ContainsKey(key))
            {
                key = control.ClientID;
            }
            else
            {
                clientIds.Add(key, "1");
            }
            return key.Replace("-", "_");
        }

        internal static string GetControlType(Type type)
        {
            return type.ToString().Replace("Vdc.Interface.", "");
        }

        public static string GetEmbeddedImageUrl(Control control, string imageName)
        {
            return control.Page.ClientScript.GetWebResourceUrl(control.GetType(), "Vdc.Interface." + GetControlType(control.GetType()) + ".resources.styles.black_glass.images." + imageName);
        }

        public static void Init(IVdcControl vdcControl)
        {
            vdcControl.ScriptManager = new ScriptManagerHelper((Control)vdcControl);
        }

        public static bool IsDesignTimeRender(Control control)
        {
            if ((control is IVdcControl) && (((IVdcControl) control).ScriptManager != null))
            {
                return (control as IVdcControl).IsDesignTimeRender;
            }
            for (Control control2 = control.Parent; control2 != null; control2 = control2.Parent)
            {
                if ((control2.Site != null) && control2.Site.DesignMode)
                {
                    return true;
                }
            }
            return false;
        }

        internal static bool IsGridType(Type t)
        {
            if (t.ToString() == "Vdc.Grid.Grid")
            {
                return true;
            }
            if (t.ToString() == "System.Object")
            {
                return false;
            }
            return IsGridType(t.BaseType);
        }

        internal static bool IsInsideGrid(Control control)
        {
            for (Control control2 = control.Parent; control2 != null; control2 = control2.Parent)
            {
                if (IsGridType(control2.GetType()))
                {
                    return true;
                }
            }
            return false;
        }

        internal static bool IsInsideGridWithCallback(Control control)
        {
            for (Control control2 = control.Parent; control2 != null; control2 = control2.Parent)
            {
                if (IsGridType(control2.GetType()) && ((bool) control2.GetType().InvokeMember("IsInCallbackHierarchy", BindingFlags.InvokeMethod, null, control2, new object[0])))
                {
                    return true;
                }
            }
            return false;
        }

        public static void RegisterClientScripts(Control control, Type type, ScriptManagerHelper scriptManager)
        {
            if (type == null)
            {
                type = GetBaseInterfaceType(control.GetType());
            }
            if (scriptManager == null)
            {
                IVdcControl control2 = control as IVdcControl;
                scriptManager = control2.ScriptManager;
            }
            if (!scriptManager.IsClientScriptIncludeRegistered("VdcCore"))
            {
                scriptManager.RegisterClientScriptInclude("VdcCore", control.Page.ClientScript.GetWebResourceUrl(type, "Vdc.Interface.VdcCore.resources.scripts.VdcCore.min.js"));
            }
            if (!scriptManager.IsClientScriptIncludeRegistered("VdcControlFactory"))
            {
                scriptManager.RegisterClientScriptInclude("VdcControlFactory", control.Page.ClientScript.GetWebResourceUrl(type, "Vdc.Interface.VdcCore.resources.scripts.VdcControlFactory.min.js"));
            }
            string controlType = GetControlType(type);
            if (!scriptManager.IsClientScriptIncludeRegistered(controlType))
            {
                scriptManager.RegisterClientScriptInclude(controlType, control.Page.ClientScript.GetWebResourceUrl(type, "Vdc.Interface." + controlType + ".resources.scripts." + controlType + ".min.js"));
            }
        }

        public static void Render(Control control, HtmlTextWriter writer)
        {
            IVdcControl control2 = control as IVdcControl;
            if (control.Page != null)
            {
                control.Page.VerifyRenderingInServerForm(control);
            }
            if (IsDesignTimeRender(control) && !control2.HasChildControlsCreated)
            {
                control2.ForceCreateChildControls();
            }
            VdcHtmlTextWriter writer2 = new VdcHtmlTextWriter(writer.InnerWriter, "");
            bool flag = true;
            if (IsInsideGridWithCallback(control))
            {
                flag = false;
                control2.ScriptContainerContent.Text = control2.CreateClientScript(true);
            }
            control2.Container.RenderControl(writer2);
            if (flag && !IsDesignTimeRender(control))
            {
                if (!CheckAjaxPageInheritance(control))
                {
                    control2.ScriptManager.RegisterStartupScript(control.ClientID, control2.CreateClientScript(false), true);
                }
                else
                {
                    writer2.Write("<script type=\"text/javascript\">" + control2.CreateClientScript(false) + "</script>");
                }
            }
        }
    }
}

